package cn.yuanqiao.common.utils;

import java.time.LocalDate;
import java.time.Year;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class DateUtilTJ {
    /**
     * 返回每一天
     * @param startDateString
     * @param endDateString
     * @return
     */
    public static List<String> getDaysBetween(String startDateString, String endDateString) {
        List<String> dates = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startDateString, formatter);
        LocalDate endDate = LocalDate.parse(endDateString, formatter);

        LocalDate currentDate = startDate;

        while (!currentDate.isAfter(endDate)) {
            dates.add(currentDate.format(formatter));
            currentDate = currentDate.plusDays(1);
        }

        return dates;
    }

    /**
     * 返回每一月
     * @param startDateString
     * @param endDateString
     * @return
     */
    public static List<String> getMonthsBetween(String startDateString, String endDateString) {
        List<String> monthRange = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate start = LocalDate.parse(startDateString, formatter);
        LocalDate end = LocalDate.parse(endDateString, formatter);

        YearMonth startMonth = YearMonth.from(start);
        YearMonth endMonth = YearMonth.from(end);

        while (!startMonth.isAfter(endMonth)) {
            String monthString = startMonth.format(DateTimeFormatter.ofPattern("yyyy-MM"));
            monthRange.add(monthString);

            startMonth = startMonth.plusMonths(1);
        }

        return monthRange;
    }

    /**
     * 返回每一年
     * @param startDateString
     * @param endDateString
     * @return
     */
    public static List<String> getYearsBetween(String startDateString, String endDateString) {
        List<String> monthRange = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate start = LocalDate.parse(startDateString, formatter);
        LocalDate end = LocalDate.parse(endDateString, formatter);

        Year startMonth = Year.from(start);
        Year endMonth = Year.from(end);

        while (!startMonth.isAfter(endMonth)) {
            String monthString = startMonth.format(DateTimeFormatter.ofPattern("yyyy"));
            monthRange.add(monthString);

            startMonth = startMonth.plusYears(1);
        }

        return monthRange;
    }

    /**
     * 返回当前月的第一天
     * @return
     */
    public static String firstDayOfMonth(){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate today = LocalDate.now();
        LocalDate firstDayOfMonth = today.withDayOfMonth(1);
        String firstDayOfMonthStr = firstDayOfMonth.format(formatter);
        return firstDayOfMonthStr;
    }

    /**
     * 返回当前时间
     * @return
     */
    public static String nowDay(){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate today = LocalDate.now();
        String firstDayOfMonthStr = today.format(formatter);
        return firstDayOfMonthStr;
    }

    /**
     * 判断日期是否在一个月
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameMonth(String date1, String date2) {
        LocalDate lDate1 = LocalDate.parse(date1, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate lDate2 = LocalDate.parse(date2, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        if(lDate1.getYear() == lDate2.getYear() && lDate1.getMonth() == lDate2.getMonth()){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 判断日期是否在一个月
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameYear(String date1, String date2) {
        LocalDate lDate1 = LocalDate.parse(date1, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate lDate2 = LocalDate.parse(date2, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        if(lDate1.getYear() == lDate2.getYear()){
            return true;
        }else {
            return false;
        }

    }
}

